package com.leyou.item.sevice.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.LyExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.sevice.BrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class BrandServiceImpl implements BrandService{

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public PageResult<BrandDTO> queryPage(int page, int rows, String sortBy, boolean desc, String search) {
        //1.分页查询
        PageHelper.startPage(page,rows);

        Example example = new Example(Brand.class);
        //2.条件查询
        if(StringUtils.isNotBlank(search)){
            example.createCriteria().orLike("name","%"+search+"%").
                    orEqualTo("letter",search);
        }
        //3.排序查询
        if(StringUtils.isNotBlank(sortBy)){
            String orderClause = sortBy + (desc ? " DESC":" ASC");
            example.setOrderByClause(orderClause);
        }
        List<Brand> list = brandMapper.selectByExample(example);

        if(CollectionUtils.isEmpty(list)){
            throw new LyException(LyExceptionEnum.BRAND_NOT_FOUND);
        }

        //通过分页插件处理查询结果
        PageInfo info = new PageInfo(list);

        //将Brand转为BrandDTO返回
        List<BrandDTO> dtoList = BeanHelper.copyWithCollection(list, BrandDTO.class);
        return new PageResult<>(info.getTotal(),dtoList);
    }

    @Override
    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        //保存品牌
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        int count = brandMapper.insertSelective(brand);
        if(count!=1){
            throw new LyException(LyExceptionEnum.INSERT_OPERATION_FAIL);
        }

        //保存品牌和分类关联关系
        count = brandMapper.insertBrandCategory(cids,brand.getId());

        if (count!=cids.size()) {
            throw new LyException(LyExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Override
    public BrandDTO queryById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if(brand==null){
            throw new LyException(LyExceptionEnum.BRAND_NOT_FOUND);
        }

        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }

    @Override
    public List<BrandDTO> queryByCategoryId(Long id) {
        List<Brand> brandList = brandMapper.selectByCategoryId(id);

        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(LyExceptionEnum.BRAND_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(brandList,BrandDTO.class);
    }

    @Override
    public List<BrandDTO> queryBrandByIds(List<Long> ids) {
        List<Brand> brandList = brandMapper.selectByIdList(ids);
        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(LyExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brandList,BrandDTO.class);
    }
}
